// pages/merchant/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{getTel,inqTel,addShop} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    telbutton:true,
    box_height_show:false,

    fileList: [],
    fileUrl:[],
    // 营业时间
    business_show:false,
    currentDate1: '8:00',
    currentDate2: '18:00',
    minHour: 0,
    maxHour: 23,
    form:{
      name:'',
      tel:'',
      store_name:'',
      // business_time:'',
      site:'',
      site_detail:'',
      brief:'',
      lat:'',
      lng:''
    }

  },

  isSotre(){
    wx.setStorageSync('isSotre', true)
  },

  timeChange1(e){
    this.setData({
      currentDate1:e.detail.value,
    })
},
timeChange2(e){
    this.setData({
      currentDate2:e.detail.value
    })
},

  formSubmit(){
    if(!this.data.form.name){
      return showText('请填写名字！',2)
    }else if(!this.data.form.tel){
      return showText('请填写手机号！',2)
    }else if(!this.data.form.store_name){
      return showText('请填写店铺名字！',2)
    }else if(!this.data.form.site){
      return showText('请填写店铺地址！',2)
    }else if(!this.data.form.site_detail){
      return showText('请填写详细地址！',2)
    }else if(!this.data.form.brief){
      return showText('请填写店铺简介！',2)
    }
    var images = ''
    if(this.data.fileUrl.length !== 0){
      var arr = []
      this.data.fileUrl.forEach((item)=>{
        arr.push(item.url)
      })
      images = arr.join(',')
    }

    var obj = {
      uid:wx.getStorageSync('uid'),
      uname:this.data.form.name,
      name:this.data.form.store_name,
      times: this.data.currentDate1 + '~' + this.data.currentDate2,
      addr:this.data.form.site,
      address:this.data.form.site_detail,
      mobile:this.data.form.tel,
      images,
      lat:this.data.form.lat,
      lng:this.data.form.lng
    }
    addShop(obj).then(res=>{
      if(res.sta == 1){
        showText(res.msg,1)
        this.setData({
          active:1,
          box_height_show:true
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000);
      }else{
        showText(res.msg,2)
      }
    })

  },
  // 手机号
  getPhoneNumber(e){
    const that = this
    var uid = wx.getStorageSync('uid')
    inqTel(uid).then(res=>{
      if(res.sta == 0){
        showText('用户信息有误！', 2)
      }else if(res.sta == 1){
        wx.login({
          success:(data)=>{
            if(data.code){
              var obj = {
                code:data.code,
                encryptedData:e.detail.encryptedData,
                iv:e.detail.iv,
                uid:wx.getStorageSync('uid')
              }
              getTel(obj).then(confing=>{
                if(confing.sta == 1){
                  that.setData({
                    'form.tel':confing.msg,
                    telbutton:false
                  })
                }
              })
            }else{
              showText('获取code失败！',2)
            }
          }
        })
      }else if(res.sta == 2){
        that.setData({
          'form.tel':res.msg,
          telbutton:false
        })
      }else{
        showText('未知错误！', 2)
      }
    })
  },

  // 地址
  getsite(){
    const that = this
    wx.getLocation({
      type: "wgs84",
      success(res) {
          wx.chooseLocation({
              latitude: res.latitude,
              longitude: res.longitude,
              success: function(data){
                console.log(data)
                if(data.address){
                  that.setData({
                    'form.site': data.address + data.name,
                    'form.lat':data.latitude,
                    'form.lng':data.longitude,
                    // 'form.site_detail':data.name? data.name : ''
                  })
                }
                // console.log(that.data.form.site)
              }
          })
      }
  })
  },
  deleteFn(e){
    var num1 = this.data.fileList.splice(1,e.detail.index)
    var num2 = this.data.fileUrl.splice(1,e.detail.index)
    this.setData({
      fileList:num1,
      fileUrl:num2,
    })
  },
  // 上传
  afterRead(event) {
    const that = this
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://horse.molijuhe.com/api/common/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        var data = JSON.parse(res.data)
        console.log(data)
        // 上传完成需要更新 fileList
        const fileList = that.data.fileList
        fileList.push({ url: data.data.fullurl });
        const fileUrl = that.data.fileUrl
        fileUrl.push({ url: data.data.url });
        that.setData({ fileList,fileUrl });
      },
      fail(res){
        showText(res.msg,2)
      }
    });
  },

  // true false常规
  click(key){
    onClick(key,this)
  },
  input(key){
    onInput(key,this)
  },
  navtab(e){
    navToTab(e)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var uid = wx.getStorageSync('uid')
    this.setData({
      active:options.id
    })
    inqTel(uid).then(res=>{
      if(res.sta == 2){
        that.setData({
          'form.tel':res.msg,
          telbutton:false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})